import 'dart:convert';

import 'package:get/get.dart';

import '../../type.dart';

///url
extension UrlEx on String {
  Uri get url {
    return Uri.parse(this);
  }

  Map<String, dynamic> get parm {
    var s = this;
    if (contains('?')) {
      s = split('?').last;
    }
    var l = s.split('&');
    Map<String, dynamic> args = {};
    for (var e in l) {
      var ll = e.split('=');
      args[ll.first] = ll.last;
    }
    return args;
  }
}

///json
extension Json on String {
  JSON? get json {
    try {
      return jsonDecode(this);
    } catch (e) {
      return null;
    }
  }

  List<T>? jsonList<T>() {
    try {
      return (jsonDecode(this) as List?)?.cast<T>();
    } catch (e) {
      return null;
    }
  }
}

/// chinese to num
extension StringChinese on String {
  int? get numeral {
    const numberMap = {
      '一': 1,
      '二': 2,
      '三': 3,
      '四': 4,
      '五': 5,
      '六': 6,
      '七': 7,
      '八': 8,
      '九': 9,
      '十': 10,
    };
    return numberMap[this];
  }
}

extension FileEx on String {
  bool get isImage {
    final path = url.path.toLowerCase();
    return path.endsWith('.heic') ||
        path.endsWith('.png') ||
        path.endsWith('.jpg') ||
        path.endsWith('.jpeg') ||
        path.endsWith('.gif') ||
        path.endsWith('.webp');
  }

  bool get isVideo {
    final ext = url.path.toLowerCase();
    return ext.endsWith(".mp4") ||
        ext.endsWith(".avi") ||
        ext.endsWith(".wmv") ||
        ext.endsWith(".rmvb") ||
        ext.endsWith(".mpg") ||
        ext.endsWith(".mpeg") ||
        ext.endsWith(".3gp") ||
        ext.endsWith('.mov') ||
        ext.endsWith('.flv');
  }
}
